import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router } from "@angular/router"; //路由引入
import {config} from '../../shared/smartadmin.config';

declare var $: any;
@Component({
  selector: 'sa-api-log',
  templateUrl: './api-log.component.html',
})
export class ApiLogComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	userName:string = '';//查询用户名称
	apiName:string = '';//查询api接口
	startTime:string = '';//开始时间
	endTime:string = '';//结束时间
	constructor(private jsonApiService: JsonApiService, private router: Router) {}

	ngOnInit() {
		this.initDate().then(()=>this.load());
	}
	//初始化时间
	initDate(){
		var that = this;
		return new Promise(function (resolve, reject) {
			System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(
				()=>{
					System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
						$("#datetimepicker1").datetimepicker({
							format: "yyyy-mm-dd hh:ii:ss",
						    language: "zh-CN",
						    autoclose: true,
						}).on("changeDate", function(e){
							var date = e.date;
							var time = common.getTimeMmss(date);
							$("#datetimepicker2").datetimepicker("setStartDate", time);
						});
						$("#datetimepicker2").datetimepicker({
							format: "yyyy-mm-dd hh:ii:ss",
						    language: "zh-CN",
						    autoclose: true,
						}).on("changeDate", function(e){
							var date = e.date;
							var time = common.getTimeMmss(date);
							$("#datetimepicker1").datetimepicker("setEndDate", time);
						});
						var date = new Date();
                        let time1 = common.getTimeMmss(date);
                        $("#datetimepicker2").val(time1); //当前时间
                        that.endTime = time1;
                        date.setTime(date.getTime() - 1*60*60*1000); //设置前一小时
                        let time2 = common.getTimeMmss(date);
                        $("#datetimepicker1").val(time2);
                        that.startTime = time2;
						resolve();
					});
				}
			);
		})
	}
	//删除所选时间
	del_date(){
		this.startTime = '';
		this.endTime ='';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker2").val('').change();
		$("#datetimepicker2").datetimepicker("setStartDate", null);
		$("#datetimepicker1").datetimepicker("setEndDate", null);
	}
	//刷新
	refresh(){
		this.load();
	}
	//表格搜索
	table_search() {
		this.curPage = 1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//初始化加载list数据
	load() {
		this.startTime = $('#datetimepicker1').val();
		this.endTime = $('#datetimepicker2').val();
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				searchKey:this.searchKey,
				userName:this.userName,
				apiName:this.apiName,
				startDate:this.startTime,
				endDate:this.endTime
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("log/logapi.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	// 数据导出
	exportData(){
		this.startTime = $('#datetimepicker1').val();
		this.endTime = $('#datetimepicker2').val();
			
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				searchKey:this.searchKey,
				userName:this.userName,
				apiName:this.apiName,
				startDate:this.startTime,
				endDate:this.endTime,
				export:"1"
			}
		}
		let timeCheck = common.correctTime(data.parameters, ['export'], ['startDate', 'endDate'])
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		common.exprotDataFile(data, 'gridPager', config.BaseURL + 'log/logapi.html')	
    }
}
